package org.example.partitioner2;


import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

/**
 * @ClassName FlowReducer
 * @Author 小坏
 * @Date 2021/7/16、17:58
 * @Version 1.0
 * <p>
 * 1、Reducer的 k,v
 * 就对应这 map的输出 Text(电话号码), FlowBean(Bean对象)
 * <p>
 * 2、最终输出的也是 Text, FlowBean
 */
public class FlowReducer extends Reducer<Text, FlowBean, Text, FlowBean> {

    private FlowBean outV = new FlowBean();

    @Override
    protected void reduce(Text key, Iterable<FlowBean> values, Context context) throws IOException, InterruptedException {

        //TODO  1、遍历集合累加值
        long totalUp = 0;
        long totalDown = 0;
        for (FlowBean value : values) {
            totalUp += value.getUpFlow();
            totalDown += value.getDownFlow();
        }

        //TODO 2、封装输出的outK, outV
        outV.setUpFlow(totalUp);
        outV.setDownFlow(totalDown);
        outV.setSumFlow();

        //TODO 3、写出
        context.write(key, outV);
    }
}
